package cn.itcast.controller;

import cn.itcast.domain.Courier;
import cn.itcast.domain.Standard;
import cn.itcast.service.CourierService;
import cn.itcast.service.StandardService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CourierController {

    @Autowired
    private CourierService courierService;

    @Autowired
    private StandardService standardService;

    /**
     * 保存/添加(有id就是修改，没有id就是添加)
     * @param courier
     * @return
     */
    @RequestMapping("/courier_save")
    public String save(Courier courier){

        courierService.save(courier);

        return "redirect:/pages/base/courier.html";
    }


    /**
     * 条件查询/分页查询
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/courier_pageQuery")
    @ResponseBody
    public Map pageQuery(int page,int rows,Courier courier){

        //封装page对象
        Pageable pageable = PageRequest.of(page - 1, rows);

        //封装查询条件  root:查询的根对象(查询的任何属性都可以从根对象中获取)
        //criteriaQuery:顶层查询对象，自定义查询方式(一般不用)
        //criteriaBuilder:查询的构造器，封装了很多的查询条件
        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                //创建一个集合用来存放查询条件
                List<Predicate> list = new ArrayList<Predicate>();

                if (courier.getCourierNum() != null && StringUtils.isNotBlank(courier.getCourierNum())) {
                    Predicate p1 = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
                    list.add(p1);
                }

                if (courier.getType() != null && StringUtils.isNotBlank(courier.getType())) {
                    Predicate p2 = cb.equal(root.get("type").as(String.class), courier.getType());
                    list.add(p2);
                }

                if (courier.getCompany() != null && StringUtils.isNotBlank(courier.getCompany())) {
                    Predicate p3 = cb.equal(root.get("company").as(String.class), courier.getCompany());
                    list.add(p3);
                }


                Join<Courier, Standard> standardJoin = root.join("standard", JoinType.INNER);

                if (courier.getStandard() != null && StringUtils.isNotBlank(courier.getStandard().getName())) {
                    Predicate p4 = cb.like(standardJoin.get("name").as(String.class), "%" + courier.getStandard().getName() + "%");
                    list.add(p4);
                }

                return cb.and(list.toArray(new Predicate[0]));
            }
        };


        Page<Courier> couriers = courierService.findAllPage(specification,pageable);
        Map<String,Object> result = new HashMap<String, Object>();

        result.put("total", couriers.getTotalElements());
        result.put("rows",couriers.getContent());
        System.out.println(couriers.getTotalElements());
        System.out.println(couriers.getTotalPages());
        return result;
    }


    /**
     * 查询所有standard
     * @return
     */
    @RequestMapping("/standard_findAll")
    @ResponseBody
    public List<Standard> findStandardId(){

        List<Standard> standards = standardService.findAll();

        return standards;
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/courier_delBatch")
    public String delete(String ids){

        String[] arr = ids.split(",");

        courierService.delete(arr);

        return "redirect:pages/base/courier.html";
    }


    /**
     * 还原删除
     * @return
     */
    @RequestMapping("/courier_RestoreBatch")
    public String restore(String ids){

        String[] arr = ids.split(",");

        courierService.restore(arr);

        return "redirect:pages/base/courier.html";
    }



}
